<?php 

namespace app\admin\event;

use app\common\enum\BaseEnum;
use app\common\model\Spec as SpecModel;
use app\common\model\SpecItem as SpecItemModel;
use app\common\utils\JsonService;

class SpecEvent{

    /*
    * 商品规格列表
    */
    public static function specList(){
        $params = array_filter(request()->param());
        $page = ! empty($params['page']) && $params['page'] >= 1 ? $params['page'] : 1;
        $where = [];
        if($id = isset($params['id'])){
                    $where[] = [['id','=',$params['id']]];
            }
            if($type_id = isset($params['type_id'])){
                    $where[] = [['type_id','like',"%{$params['type_id']}%"]];
            }
            if($name = isset($params['name'])){
                    $where[] = [['name','like',"%{$params['name']}%"]];
            }
            if($order = isset($params['order'])){
                    $where[] = [['order','like',"%{$params['order']}%"]];
            }
             $condition = [
                'with'=>['getGoodsType','getSpecItems'],
                'where' => $where,
                'order' => ['id' => 'desc'],
                    'paginate' => [
                    'page' => $page,
                    'query' => $params,
                ],
        ];
        $spec = SpecModel::getPageAll($condition)->each(function($item){
            $item['search_index'] = BaseEnum::getBoolValue($item['search_index']);
            $item['specItem'] = '';
            if(!empty($item['getSpecItems'])){
                $tmp = array_column($item->getSpecItems->toArray(),'item');
                $item['specItem'] = implode(',',$tmp);
            }
        });
        
        return $spec;
    }


    /*
    * 新增商品规格
    */
    public static function createSpec($data){
        $specItem = isset($data['specItem'])?$data['specItem']:[];
        SpecModel::beginTrans();
        try {
            $res = SpecModel::create($data);
            if(!empty($specItem)){
                foreach($specItem as $k=>$v){
                    $insertSpecItem[] = [
                        'spec_id'=>$res->id,
                        'item'=>$v,
                    ];
                }
                SpecItemModel::setAll($insertSpecItem);
            }
            SpecModel::commitTrans();
        } catch (\Exception $ex) {
            SpecModel::rollbackTrans();
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }

    /*
    * 更新商品规格
    */
    public static function updateSpec($data){
        $specItem = isset($data['specItem'])?$data['specItem']:[];

        SpecModel::beginTrans();
        try {
            if(empty($specItem['update']) && empty($specItem['add'])){
                throw new \Exception('规格值不能为空');
            }

            if(!empty($specItem['update'])){
                foreach($specItem['update'] as $k=>$v){
                    SpecItemModel::updateOne(['item'=>$v],['id'=>$k]);
                }
            }

            if(!empty($specItem['add'])){
                $insert = [];
                foreach ($specItem['add'] as $v) {
                    if (empty($v)) {
                        continue;
                    }
                    $insert[] = [
                        'spec_id' => $data['id'],
                        'item' => $v,
                    ];
                }
                SpecItemModel::setAll($insert, true);
            }
            SpecModel::updateOne($data, ['id' => $data['id']]);
            SpecModel::commitTrans();
        } catch (\Exception $ex) {
            SpecModel::rollbackTrans();
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }


}

